
package com.wwy.monitor.service.impl;

import com.wwy.monitor.model.vo.MailMessageVO;
import com.wwy.monitor.service.MonitorMailService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: </p>
 * @date 2019-11-04 11:10
 */
@Service(value = "monitorMailService")
public class MonitorMailServiceImpl implements MonitorMailService {
    @Resource(name = "mailMcService")
    private MailMcService mailMcService;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public boolean sendMail(String userId, String mailAddress, String mailTitle, String templateName
            , Map<String, Object> paramMap, boolean isHtml) throws IOException, TemplateException {
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
// åéé®ä»¶
        MailMessageVO mailMessageVO = new MailMessageVO();
        mailMessageVO.setToMailList(Arrays.asList(mailAddress));
        mailMessageVO.setMailTitle(mailTitle);
        mailMessageVO.setMailText(FreeMarkerTemplateUtils.processTemplateIntoString(template, paramMap));
        mailMessageVO.setHtmlText(isHtml);
        return mailMcService.sendMsg("system", mailMessageVO);
    }
}